$(function () {
	$.fn.datebox.defaults.formatter = function(date){
		var y = date.getFullYear();
		return ''+y;
	};
	
	$('#panel').panel({
	    title:'商品类别销售统计',
	}); 
	
	$('#year').datebox({
		required:true,
	});
	
	Highcharts.setOptions({
		lang:{
			loading:'Loading...',
			printChart: '打印',
			downloadJPEG: '导出JPEG',
			downloadPDF: '导出PDF',
			downloadPNG: '导出PNG',
			downloadSVG: '导出SVG',
		}
	});
	
//	折线图
	$('#line').click(function(){
		var year = $('#year').datebox('getValue');
		var url = 'highcharts.action?year='+year;
		getLineChart(url);//获取折线图
	});
	
//	条形图
	$('#chart').click(function(){
		var year = $('#year').datebox('getValue');
		var url = 'highcharts.action?year='+year;
		getChart(url);//获取折线图
	});
	
	
	function getLineChart(url){
		$.getJSON(url, function (data) {
	        $('#container').highcharts({
	        	chart: {
	                type: 'line'
	            },
	            title: {
	                text: '月销售总额统计'
	            },
	            credits: {
	                enabled:false
	            },
	            subtitle: {
	                text: '年份:'+data[0].year
	            },
	            tooltip: {
	                shared: true,
	                crosshairs: true
	            },
	            xAxis: {
//	                categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
	                categories: data[0].category
	            },
	            yAxis: {
	                title: {
	                    text: '销售额 （单位：元）'
	                }
	            },
	            plotOptions: {
	                line: {
	                    dataLabels: {
	                        enabled: true
	                    },
	                    enableMouseTracking: true
	                }
	            },           
	            series: data
	        });
	    });
	}
	function getChart(url){
		$.getJSON(url, function (data) {
			$('#container').highcharts({
				chart: {
					type: 'column'
				},
				title: {
					text: '月销售总额统计'
				},
				credits: {
					enabled:false
				},
				subtitle: {
					text: '年份:'+data[0].year
				},
				tooltip: {
					shared: true,
					crosshairs: true
				},
				xAxis: {
					categories: data[0].category
				},
				yAxis: {
					title: {
						text: '销售额 （单位：元）'
					}
				},
				plotOptions: {
					line: {
						dataLabels: {
							enabled: true
						},
						enableMouseTracking: true
					}
				},           
				series: data
			});
		});
	}
	
    
});